import React, {PureComponent} from "react";
import {Table} from "antd";
import {connect} from "react-redux";
import {tableProps} from "../../lib/ui";
import {query} from "./actions";
import {getLastFilter} from "../../lib/identity";

const LAST_FILTER = "userOperation";

class List extends PureComponent {

    componentWillMount() {
        const {dispatch} = this.props;
        let fields = getLastFilter(LAST_FILTER);
        if (fields === null || fields === undefined) {
            fields = {};
        }
        dispatch(query({...fields}));
    };

    createNewData = data => {
        return data.reduce((result, item) => {
            //首先将organizationName字段作为新数组result取出
            if (result.indexOf(item.organizationName) < 0) {
                result.push(item.organizationName)
            }
            return result
        }, []).reduce((result, organizationName) => {
            //将name相同的数据作为新数组取出，并在其内部添加新字段**rowSpan**
            const children = data.filter(item => item.organizationName === organizationName);
            result = result.concat(
                children.map((item, index) => ({
                    ...item,
                    rowSpan: index === 0 ? children.length : 0,//将第一行数据添加rowSpan字段
                }))
            )
            return result;
        }, [])
    };

    render() {
        const {
            dataSource,
            loading,
        } = this.props;

        const columns = [
            {
                title: "组织机构",
                dataIndex: "organizationName",
                width: "150px",
                render: (text, record) => {
                    return {
                        children: record.organizationName,
                        props: {
                            rowSpan: record.rowSpan,
                        }
                    }
                }
            }, {
                title: "帐号",
                dataIndex: "userName",
                width: "150px",
            }, {
                title: "单量",
                dataIndex: "count",
                width: "120px"
            }, {
                title: "组织机构总单量",
                dataIndex: "orgCount",
                width: "120px",
                render: (text, record) => {
                    return {
                        children: record.orgCount,
                        props: {
                            rowSpan: record.rowSpan,
                        }
                    }
                }
            }, {
                title: "",
                dataIndex: ""
            }
        ];
        //合并数组单元格


        return (
            <Table
                {...tableProps}
                columns={columns}
                scroll={{x: 1500, y: 'calc(100vh - 380px)'}}
                dataSource={this.createNewData(dataSource)}
                pagination={false}
                loading={loading}
            />
        );
    }
}

const
    mapStateToProps = state => {
        return {
            ...state.userOperation.list
        };
    };

export default connect(mapStateToProps)(List);
